// pages/classHomePage/qiangda/index/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    people: null,
    all_qiangda_list: null,
    title: null,
    answer: null,
    input_title: null,
    input_answer: null,
    funList: {
      'funitems': [{
          "name": "PK",
          "url": "/pages/classHomePage/pk/index/index",
          "pic_url": "/assets/wx_pic/menu_pk@3x.png"
        },
        {
          "name": "盲盒",
          "url": "/pages/classHomePage/blindbox/index/index",
          "pic_url": "/assets/wx_pic/menu_mh@3x.png"
        },
        {
          "name": "签到",
          "url": "/pages/classHomePage/signIn/index/index",
          "pic_url": "/assets/wx_pic/menu_qd@3x.png"
        },
        {
          "name": "辩论会",
          "url": "/pages/classHomePage/debate/index/index",
          "pic_url": "/assets/wx_pic/menu_blh@3x.png"
        },
        {
          "name": "抢答",
          "url": "/pages/classHomePage/qiangda/index/index",
          "pic_url": "/assets/wx_pic/menu_qd_1@3x.png"
        },
        {
          "name": "讨论",
          "url": "/pages/classHomePage/discuss/index/index",
          "pic_url": "/assets/wx_pic/menu_tl@3x.png"
        },
        {
          "name": "成员分组",
          "url": "/pages/classHomePage/group/index/index",
          "pic_url": "/assets/wx_pic/menu_zyfz@3x.png"
        },
        {
          "name": "积分",
          "url": "/pages/classHomePage/integral/index/index",
          "pic_url": "/assets/wx_pic/menu_jf@3x.png"
        },
      ]
    },
    card: [{
      title: {
        text: "抢答",
        color: "#272635" //字体颜色
      },
      tag: {
        text: "1小时前"
      },
      header: {
        bgcolor: "#f5f5f5", //#f5f5f5  #fbde35
        line: true
      }
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    _this.setData({
      people: wx.getStorageSync("people")
    })
    _this.get_all_qiangda()
    if (wx.getStorageSync("people") == 'student') {
      _this.get_all_qiangda().then(res => {
        for (let index = 0; index < res.length; index++) {
          if (res[index]['isFinished'] == 0) {
            console.log('可以抢答！！！！！！！！！', res);
            wx.navigateTo({
              url: '/pages/classHomePage/qiangda/doexam/doexam?qiangda_id=' + String(res[index]['id']),
            })
          }
        }
      })
      // clearInterval(id)
    }
  },

  get_all_qiangda: function () {
    let _this = this
    return new Promise(function (resolve, reject) {
      let data = {}
      if (wx.getStorageSync("people") == 'student') {
        data = {
          'course_name': app.globalData.banji_info['className'],
          'teacher_id': app.globalData.banji_info['teacher_id']
        }
      } else if (wx.getStorageSync("people") == 'teacher') {
        data = {
          'course_name': app.globalData.banji_info['courseName'],
          'teacher_id': app.globalData.banji_info['teacherId']
        }
      }
      app.formPost('/get_all_qiangda?', data).then(res => {
        console.log('查询所有抢答结果/get_all_qiangda：', res);
        _this.setData({
          all_qiangda_list: res
        })
        resolve(res)
      })
    })
  },

  inputTitle(e) {
    this.setData({
      title: e.detail.value
    })
  },
  inputAnswer(e) {
    this.setData({
      answer: e.detail.value
    })
  },

  teacher_add_qiangda: function () {
    let _this = this
    let data = {
      'course_name': app.globalData.banji_info['courseName'],
      'qiangda_type': _this.data.title,
      'question': _this.data.title,
      'reference_answer': _this.data.answer,
      'teacher_id': app.globalData.teacher_info['id']
    }
    // console.log(_this.data.title);
    if (_this.data.title == null || _this.data.answer == null || _this.data.title == '' || _this.data.answer == '') {
      console.log('输入不能为空');
      app.message('题目和答案输入不能为空', 'error')
    } else {
      _this.setData({
        input_title: null,
        input_answer: null
      })
      app.formPost('/add_qiangda?', data).then(res => {
        console.log('创建抢答结果：', res);
        _this.get_all_qiangda()
      })
    }

  },

  teacher_close_qiangda: function (e) {
    let _this = this
    // console.log(e);
    let id = e.target.dataset['id']
    let data = {
      'qiangda_id': id
    }
    app.formPost('/close_qiangda', data).then(res => {
      console.log('关闭抢答结果：', res);
    })
  },

  gotoQiangdaMessage: function (e) {
    console.log(e.currentTarget.dataset);
    wx.navigateTo({
      url: '/pages/classHomePage/qiangda/qiangdaMessage/qiangdaMessage?qiangda_id=' + String(e.currentTarget.dataset['id']),
    })
  },

  click_item: function (e) {
    if (e.currentTarget.dataset.url != '') {
      wx.navigateBack()
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})